/*
 * @Author: ch cwl_ch@163.com
 * @Date: 2022-12-28 14:42:03
 * @LastEditors: ch
 * @LastEditTime: 2022-12-28 14:54:32
 * @Description: file content
 */

import { _ToAsyncAwait } from '@gykeji/jsutil';
import MAP_CON from '../config/gdMapConf';
const formatCity = (data)=>{
    let arr = [];
    data.forEach(i => {
        if(i.citycode.length){
            arr.push(i);
        }
        if(i.districts.length){
            arr = arr.concat(formatCity(i.districts))
        }
    });
    return arr;
}
/**
 * @Description: 获取城市列表，直接使用高德API
 * @return {*}
 */
const ApiGetCityList = () => _ToAsyncAwait(
    new Promise((res, rej) => {
        uni.request({
            method: 'GET',
            url: `${MAP_CON.cityApiUrl}?subdistrict=2&key=${MAP_CON.cityKey}`,
            success(result){
                res(
                    formatCity(result.data.districts[0].districts).sort((a,b)=>{
                        return a.name.localeCompare(b.name, 'zh-CN');
                    })
                );
            },
            error(e){
                rej(e)
            }
        })
    })
)


const ApiReverseGeocode = ({ lat, lng }) => _ToAsyncAwait(
    new Promise((res, rej) => {
        uni.request({
            method: 'GET',
            url: `https://restapi.amap.com/v3/geocode/regeo`,
            data: {
                location: `${lng},${lat}`,
                key: MAP_CON.cityKey,
            },
            success(result) {
                const data = result.data;
                if (data.status === '1' && data.regeocode) {
                    const component = data.regeocode.addressComponent;
                    res({
                        adcode: component.adcode,
                        citycode: component.citycode,
                        name: component.city || component.province,
                        center: `${lng},${lat}`,
                    });
                } else {
                    rej(new Error('高德逆地理编码失败'));
                }
            },
            fail(err) {
                rej(err);
            }
        });
    })
);


export {
    ApiGetCityList,
    ApiReverseGeocode
}